// pages/order/order.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unread: 2,
    tabList: ['待派单', '待接单', '进行中/待核销', '已完成', '售后订单'], // '接单池',
    orderState: 1,
    list: [],
    finish: false,
    loading: false,
    page: 1,
    active: 0,
    token: wx.getStorageSync('token')
  },
  notice() {

  },
  orderBtnTap(e) {
    debugger;
    if (e.detail.type == 1) {
      wx.requestSubscribeMessage({
        tmplIds: [app.globalData.templateconfig.shop_order_template, app.globalData.templateconfig.shop_finish_template, app.globalData.templateconfig.shop_sales_template],
        complete: (res) => {
          http.get('order/shopaccept', {
            id: e.detail.info.id
          }, true, false).then(res => {
            this.setData({
              active: 1
            })
            this.reload()
            util.toast('抢单成功，请派单')
          }).catch(e => {
            let data = e.data
            if (data.code === 2) {
              util.toast(data.msg)
              setTimeout(() => {
                util.skip('/service/earnestMoney/earnestMoney')
              }, 1000)
            } else if (data.code === 0) {
              util.toast(data.msg)
            }
          })
        }
      })

    } else if (e.detail.type == 2) {
      util.skip('/pages/allocation/allocation?id=' + e.detail.info.id)
    } else if (e.detail.type == 3) {
      wx.openLocation({
        latitude: Number(e.detail.info.address.lat),
        longitude: Number(e.detail.info.address.lng),
        name: e.detail.info.address.area,
        scale: 18
      })
    }
  },
  orderTap(e) {
    util.skip('/pages/orderDetail/orderDetail?order_id=' + e.detail.id)
  },
  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    if (!wx.getStorageSync('token')) return
    const {
      tabList,
      active
    } = this.data;
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
    }
    //接单池 '待派单','待接单','进行中/待核销','已完成','售后订单'
    // if(this.data.active === 0){
    //   data['is_pool'] = 1
    // }else if(this.data.active === 1){
    //   data['not_allocation'] = 1
    // }else if(this.data.active === 2){
    //   data['not_accept'] = 1
    // }else if(this.data.active === 3){
    //   data['not_finish'] = 1
    // }else if(this.data.active === 4){
    //   data['finish'] = 1
    // }else if(this.data.active === 5){
    //   data['in_service'] = 1
    // }
    if (tabList[active] === '接单池') {
      data['is_pool'] = 1
    } else if (tabList[active] === '待派单') {
      data['not_allocation'] = 1
    } else if (tabList[active] === '待接单') {
      data['not_accept'] = 1
    } else if (tabList[active] === '进行中/待核销') {
      data['not_finish'] = 1
    } else if (tabList[active] === '已完成') {
      data['finish'] = 1
    } else if (tabList[active] === '售后订单') {
      data['in_service'] = 1
    }
    http.get('order/shoporderlist', data).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  //派单
  toallocation() {
    wx.navigateTo({
      url: '/pages/allocation/allocation',
    })
  },
  toDetail() {
    wx.navigateTo({
      url: '../orderDetail/orderDetail',
    })
  },
  onChange(event) {
    this.setData({
      active: event.detail.index
    })
    this.reload()
  },
  getOrderGrabConfig() {
    let {
      tabList
    } = this.data;
    http.get('order/grabConfig', {}).then(res => {
      console.log(res)
      if (res.data && res.data.grab_open === 1) {
        tabList.unshift('接单池')
        this.setData({
          tabList
        })
      }
    }).finally(res => {
      
    })
    // service/order/grabConfig
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.reload()
    this.getOrderGrabConfig()
  },
})